import { _decorator, Component, Node, Label,Animation, deserializeTag, log } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('PSZ_SceneMgr')
export class PSZ_SceneMgr extends Component {
    @property(Label)
    public roomID: Label
    @property(Label)
    public gameCount: Label
    @property(Node)
    public seatList: Node[] = [];
    @property(Node)
    public readyBtnNode:Node
    @property(Node)
    public cardAnimParent:Node
    @property(Node)
    public betBtnNode:Node
    @property(Node)
    public showBtnNode:Node

    start() {
        this._init();
    }

    private _init() {
        globalThis._eventTarget.on('request_room_info', this.onRequestRoomInfo, this);
        globalThis._eventTarget.on("sync_all_player_info", this.onSyncAllPlayerInfo, this);
        globalThis._eventTarget.on("sync_all_player_ready_state", this.onSyncAllPlayerReadyState, this);
        globalThis._eventTarget.on("start_game", this.onStartGame, this);
    }

    update(deltaTime: number) {

    }

    public onRequestRoomInfo(data) {
        console.log("更新房间信息", data);

        //{room_id: 608215, create_id: 666666, game_numbers: 10, current_numbers: null}
        // data = {
        //     roomID:123456,
        //     gameCurrentCount:2,
        //     gameAllCount:8
        // }
        if (data.current_numbers === null) {
            data.current_numbers = 0;
        }
        this.roomID.string = data.room_id;
        this.gameCount.string = data.current_numbers + '/' + data.game_numbers;
    }

    onSyncAllPlayerInfo(data) {
        //设置所有的头像节点不可见
        for (let i = 0; i < this.seatList.length; i++) {
            this.seatList[i].active = false;
        }

        console.log('更新全部玩家信息');
        console.log(data);
        let mySeataIndex;      //自己座位号

        for (let i = 0; i < data.length; i++) {
            let playerInfo = data[i];
            if (playerInfo.user_id === globalThis._userInfo.user_id) {
                mySeataIndex = playerInfo.user_seatIndex;
                globalThis._userInfo.m_seatIndex = mySeataIndex;
            }
        }

        for (let i = 0; i < data.length; i++) {
            let playerInfo = data[i];
            let index = this.getLocalIndex(playerInfo.user_seatIndex, mySeataIndex, 6);
            switch (index) {
                case 0:
                    this.updataUserInfo(0,playerInfo);
                    this.seatList[0].active = true;
                    break;
                case 1:
                    this.updataUserInfo(1,playerInfo);
                    this.seatList[1].active = true;
                    break;
                case 2:
                    this.updataUserInfo(2,playerInfo);
                    this.seatList[2].active = true;
                    break;
                case 3:
                    this.updataUserInfo(3,playerInfo);
                    this.seatList[3].active = true;
                    break;
                case 4:
                    this.updataUserInfo(4,playerInfo);
                    this.seatList[4].active = true;
                    break;
                case 5:
                    this.updataUserInfo(5,playerInfo);
                    this.seatList[5].active = true;
                    break;
                default:
                    console.log('座位号计算错误');
                    break;
            }
        }
    }

    updataUserInfo(seatIndex,userData){
        let seatNode = this.seatList[seatIndex];
        let qiPaiNode = seatNode.getChildByName("cardNode").getChildByName("qipai");
        let nameLabel = seatNode.getChildByName("headNode")
                                .getChildByName("头像背景")
                                .getChildByName("nameLabel")
                                .getComponent(Label);
        //let headNode = seatNode.getChildByName("headNode");
        //console.log(headNode); 
        nameLabel.string = userData.user_name;
        if(!userData.is_fold)
        {
            qiPaiNode.active = false;
        }else{
            qiPaiNode.active = true;
        }
    }

    updataReadyInfo(seatIndex,readyState){
        let seatNode = this.seatList[seatIndex];
        let readyNode = seatNode.getChildByName("headNode")
                                .getChildByName("ready_ok");
        readyNode.active = readyState;
    }

    onSyncAllPlayerReadyState(data){
        console.log('玩家准备的数据为',data);
        let mySeataIndex;      //自己座位号
        for (let i = 0; i < data.length; i++) {
            let playerInfo = data[i];
            //获取自己的座位号
            if (playerInfo.user_id === globalThis._userInfo.user_id) {
                mySeataIndex = playerInfo.user_seatIndex;
                globalThis._userInfo.m_seatIndex = mySeataIndex;
            }
        }

        for (let i = 0; i < data.length; i++) {
            let playerInfo = data[i];           //拿到每个玩家准备信息
            //判断当前拿出玩家信息的逻辑座位号
            let index = this.getLocalIndex(playerInfo.user_seatIndex, mySeataIndex, 6);
            switch (index) {
                case 0:
                    this.updataReadyInfo(0,playerInfo.ready_state);
                    break;
                case 1:
                    this.updataReadyInfo(1,playerInfo.ready_state);
                    break;
                case 2:
                    this.updataReadyInfo(2,playerInfo.ready_state);
                    break;
                case 3:
                    this.updataReadyInfo(3,playerInfo.ready_state);
                    break;
                case 4:
                    this.updataReadyInfo(4,playerInfo.ready_state);
                    break;
                case 5:
                    this.updataReadyInfo(5,playerInfo.ready_state);
                    break;
                default:
                    console.log('座位号计算错误');
                    break;
            }
        }

    }

    getLocalIndex(otherIndex, thisIndex, playerNumbers) {
        let ret = (otherIndex - thisIndex + playerNumbers) % playerNumbers;
        return ret;
    }
    //otherIndex  服务端绝对座位号
    //thisIndex   自己的座位号（服务端）
    //playerNumbers 玩家总共的个数

    //准备按钮点击事件
    onReadyBtnClicked(){
        this.readyBtnNode.active = false;
        globalThis._PSZ_ClientMgr._sendMessage('ready_ok',{userID:globalThis._userInfo.user_id});
    }

    onCardAnimation(data){
        console.log('发牌动画');
        // let data = {
        //     playerInfoList:[{seatIndex:0},{seatIndex:1},{seatIndex:2},{seatIndex:3},{seatIndex:4},{seatIndex:5}],
        // }

        let animationNode = this.cardAnimParent.getChildByName("cardAnim").getComponent(Animation);
        let time = 0;
        for (let i = 0; i < 3; i++) {
            for (let i = 0; i < data.playerInfoList.length; i++) {
                let index = this.getLocalIndex(data.playerInfoList[i].user_seatIndex, globalThis._userInfo.m_seatIndex, 6);
                let anmiName = this.cardAnimParent.getChildByName("cardAnim").getComponent(Animation).clips[index].name
                setTimeout(() => {
                    animationNode.play(anmiName);
                }, time);
                time += 300;
            } 
        }
        setTimeout(() => {
            this.cardAnimParent.active = false;
            this.showCardNode(data);
            this.showBetBtn(data)   //显示下注按钮
        }, time);
    }

    //处理游戏开始的逻辑
    onStartGame(data){
        //先隐藏玩家的准备图标
        for(let i = 0; i < data.playerInfoList.length; i ++){
            //获取逻辑座位号   （相对自己的座位号）         
            let index = this.getLocalIndex(data.playerInfoList[i].user_seatIndex, globalThis._userInfo.m_seatIndex, 6);
            let node = this.seatList[index].getChildByName("headNode").getChildByName("ready_ok");
            node.active = false;
        }
  
        //做发牌动画  传入data数据段
        this.onCardAnimation(data);

        console.log('可以开始游戏了');
    }

    showCardNode(data){
        console.log('发牌动画结束');
        for (let i = 0; i < this.seatList.length; i++) {
            let seatNode = this.seatList[i];
            let cardNode = seatNode.getChildByName("cardNode");
            cardNode.active = true;
        }
    }

    //显示下注按钮
    showBetBtn(data){
        for(let i = 0; i < data.playerInfoList.length; i ++){
            let playerInfo = data.playerInfoList[i];
            if(playerInfo.user_id === globalThis._userInfo.user_id && playerInfo.user_id === playerInfo.current_player_id){
                this.betBtnNode.active = true;
                if(!playerInfo.is_open)
                {
                    this.showBtnNode.active = true;
                    console.log('显示下注按钮');
                }
                return;
            }
        }
    }   
}


